package com.hellobike.haxing.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hellobike.haxing.modules.order.constant.OrderConstants;
import com.hellobike.haxing.modules.order.entity.OrderDetail;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.order.mapper.OrderDetailMapper;
import com.hellobike.haxing.modules.order.service.IOrderDetailService;
import com.hellobike.haxing.modules.product.service.IPromotionFlashService;
import com.hellobike.haxing.modules.product.service.IPromotionGroupService;
import com.hellobike.haxing.modules.product.service.ISkuWarehouseStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 订单明细
 * @Author: haxing-boot
 * @Date:   2020-02-08
 * @Version: V1.0
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements IOrderDetailService {
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private IPromotionFlashService iPromotionFlashService;
    @Autowired
    private IPromotionGroupService iPromotionGroupService;
    @Autowired
    private ISkuWarehouseStockService iSkuStockService;

    @Override
    public List<OrderDetail> queryDetailList(Long orderId) {
        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrderDetail::getOrderId,orderId);
        return super.list(queryWrapper);
    }

    /**
     * 返回订单实际库存 限时购库存 拼团库存
     * @param orderId
     * @param shopId
     * @throws Exception
     */
    @Override
    public void returnOrderDetailStock(Long orderId, Long shopId) throws Exception {
        //返回订单实际库存 限时购库存 拼团库存
        List<OrderDetail> detailList = orderDetailMapper.queryOrderDetail(orderId);
        //实际库存
        Map<Long, Integer> sjStockMap = new HashMap<>();
        for (OrderDetail orderDetail : detailList) {
            sjStockMap.put(orderDetail.getSkuId(), orderDetail.getNum());
            if ((orderDetail.getBizType().equals(OrderConstants.ORDER_BIZ_TYPE_3) || orderDetail.getBizType().equals(OrderConstants.ORDER_BIZ_TYPE_4)) && null != orderDetail.getGroupId()) {
                Result result = iPromotionGroupService.changeGroupSkuStock(orderDetail.getGroupId(), orderDetail.getNum());
                if (!result.isSuccess()) {
                    throw new Exception("更新活动失败");
                }
            } else if (orderDetail.getBizType().equals(OrderConstants.ORDER_BIZ_TYPE_2) && null != orderDetail.getFlashId()) {
                Result result = iPromotionFlashService.changeFlashSkuStock(orderDetail.getFlashId(), orderDetail.getPromotionNum());
                if (!result.isSuccess()) {
                    throw new Exception("更新活动失败");
                }
            }
        }
        Result result = iSkuStockService.changeSkuStocks(sjStockMap, shopId);
        if (!result.isSuccess()) {
//            log.error("====库存返回失败=={}==", result.getMessage());
            throw new Exception("库存返回失败！");
        }

    }
}
